package com.projectframe.base.ui;

@SuppressWarnings("serial")
public class BaseForm implements java.io.Serializable
{
    private int          totalRows;     // 总行数
    private int          totalPage;     // 总页数
    private int          pageRows;      // 页行数
    private int          pageIndex;     // 当前页

    public void init(final int totalRows, final int startRow, final int pageRows)
    {
        this.totalRows = totalRows;
        this.pageRows = pageRows;
        this.totalPage = getPageSize(totalRows, pageRows);
        
        if(this.totalPage>0)
            this.pageIndex = getPageSize(startRow, pageRows);
        else
            this.pageIndex = 0;
        
        if(this.pageIndex>this.totalPage)
            this.pageIndex = this.totalPage;
    }
    
    private int getPageSize(int totalRows, int pageRows) 
    {  
        // 最大页数  
        int pageSize;  
        // 实际每页数据条数  
        int actualSize;  
        // 计算实际每页的条数,如果请求的每页数据条数大于总条数, 则等于总条数  
        actualSize = (pageRows > totalRows) ? totalRows : pageRows;  
        
        if (totalRows > 0)
            pageSize = (totalRows % pageRows == 0) ? (totalRows / actualSize) : (totalRows / actualSize + 1);  
        else 
            pageSize = 0;  
         
        return pageSize;  
    }
    
    public int getStartRow()
    {
        return pageIndex * pageRows;
    }

    public int getTotalRows()
    {
        return totalRows;
    }

    public void setTotalRows(int totalRows)
    {
        this.totalRows = totalRows;
    }

    public int getTotalPage()
    {
        return totalPage;
    }

    public void setTotalPage(int totalPage)
    {
        this.totalPage = totalPage;
    }

    public int getPageRows()
    {
        return pageRows;
    }

    public void setPageRows(int pageRows)
    {
        this.pageRows = pageRows;
    }

    public int getPageIndex()
    {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex)
    {
        this.pageIndex = pageIndex;
    }
}
